import {MyColumnCellRenderParams, MyColumnEditRenderOptions, MyColumnEditRenderParams} from "./MyTables";
import typeOfUtils from "../../../utils/type-of-utils";

export class PluginExtend {
  static optionsPlus(renderOpts: MyColumnEditRenderOptions, params: MyColumnEditRenderParams | MyColumnCellRenderParams): any[] {
    if (renderOpts.options && renderOpts.options.length) {
      return renderOpts.options
    }
    if (renderOpts.setOptions) {
      params.column.prop = params.column.field
      params.column.label = params.column.title
      renderOpts._setOptions = renderOpts.setOptions(params.$table.data, params) || []
    }
    if (renderOpts.asyncSetOptions) {
      renderOpts.asyncSetOptions(params.$table.data, params).then(res => {
        renderOpts._setOptions = res || []
      })
    }
    return renderOpts._setOptions
  }

  /**
   * 是否重新加载options
   * @param renderOpts
   * @param params
   */
  static focusLoadAgain(renderOpts: MyColumnEditRenderOptions, params: MyColumnEditRenderParams | MyColumnCellRenderParams) {
    const props = renderOpts.props
    if (typeOfUtils.isFunction(props?.focusLoadAgain)) {
      // @ts-ignore
      if (props.focusLoadAgain()) {
        if (renderOpts.asyncSetOptions) {
          renderOpts.asyncSetOptions(params.$table.data, params).then(res => {
            renderOpts._setOptions = res || []
          })
        }
      }
    }
  }
}
